package leo.weixin.controller;

import leo.weixin.common.ServerResponse;
import leo.weixin.entity.material.Article;
import leo.weixin.entity.message.TextMessage;
import leo.weixin.service.IMenuService;
import leo.weixin.service.IWxService;
import leo.weixin.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.print.attribute.standard.Media;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private IMenuService iMenuService;
    @Autowired
    private IWxService iWxService;
    /**
     * 创建自定义菜单
     * @return
     */
    @PostMapping("/menu/create")
    public String createMenu(){
        // todo 管理员后台 请求接收
        return iMenuService.createMenu();
    }
    /**
     * 查询自定义菜单
     * @return
     */
    @GetMapping("/menu/get")
    public String getMenuInfo(){
        //http请求方式: GET（请使用https协议）https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN
        // todo 管理员后台 请求接收
        return iMenuService.getMenuInfo();
    }
    /**
     * 删除自定义菜单
     * @return
     */
    @GetMapping("/menu/del")
    public String delMenu(){
        //http请求方式: GET（请使用https协议）https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
        // todo 管理员后台 请求接收
        return iMenuService.delMenu();
    }

    /**
     * 上传零时素材接口
     * @param //type
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){
        return iWxService.addMedia(file);
    }

    /**
     * 新增永久素材接口
     * @return
     */
    @PostMapping("/addMaterial")
    public String addMaterial(){
        return iWxService.addMaterial(null);
    }

    @GetMapping("/getMedia")
    public String getMedia(){
        return iWxService.getMedia(null);
    }

    /**
     * 获取二维码
     * 目前只提供获取临时的
     * @return
     */
    @GetMapping("/getQrCode")
    public ServerResponse<String> getQrCode(){
        return ServerResponse.createBySuccess(iWxService.getQrCode());
    }
    @GetMapping("/getQrCode1")
    public Map<String,Object> getQrCode1(){
        Map<String,Object> map = new HashMap<>();
        map.put("int",new Integer("15"));
        map.put("string","aaa");
        Article article = Article.builder()
                .title("title")
                .content("content")
                .contentSourceUrl("contentUrl")
                .showCoverPic(false)
                .thumbMediaId("mid")
                .build()
                .setAuthor("author")
                .setDigest("digest")
                .setOnlyFansCanComment(true)
                .setNeedOpenComment(true);
        map.put("article",article);
        return map;
    }
}
